/********************************************************************************
* File Name:	welcomeWorker.cpp
* Description:	第10章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include "welcomeWorker.h"

extern int workIndex; //记录工作人员登录用户名

//构造函数
WelcomeWorker::WelcomeWorker(QWidget* parent) : QDialog(parent)
{
    setupUi(this);
    workerAddMenuModel = new QSqlTableModel(this);
    workerAddMenuModel->setTable("menulist"); //绑定menulist表
    workerAddMenuModel->setEditStrategy(QSqlTableModel::OnManualSubmit); //OnManualSubmit表明我们要提交修改才能使其生效
    workerAddMenuModel->select(); //执行select()函数更新视图信息
    tvWorkerMenu->setModel(workerAddMenuModel); //绑定视图

    workerSearchOrderModel = new QSqlRelationalTableModel(this);
    workerSearchOrderModel->setTable("orderMenu"); //绑定orderMenu表
    workerSearchOrderModel->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit); //OnManualSubmit表明我们要提交修改才能使其生效
    workerSearchOrderModel->setRelation(1,QSqlRelation("menulist","mid","mname")); //通过mid将mname与orderMenu中的信息绑定
    workerSearchOrderModel->setRelation(2,QSqlRelation("Student","snumber","sname"));//通过snumber将sname与orderMenu中的信息绑定
    workerSearchOrderModel->select();
    tvWorkerOrder->setModel(workerSearchOrderModel); //绑定视图

    /*信号与槽函数绑定*/
    connect(submitWorkerEnter, SIGNAL(clicked()),this,SLOT(submitWorkerEnter_clicked())); //增加新的菜肴
    connect(submitWorkerMenuDelete, SIGNAL(clicked()),this,SLOT(submitWorkerMenuDelete_clicked())); //删除菜肴
    connect(submitWorkerMenuChange, SIGNAL(clicked()),this,SLOT(submitWorkerMenuChange_clicked())); //更新菜肴信息
    connect(submitWorkerMenuSearch, SIGNAL(clicked()),this,SLOT(submitWorkerMenuSearch_clicked())); //查看菜肴
    connect(submitWorkerOrderSearchAll, SIGNAL(clicked()),this,SLOT(submitWorkerOrderSearchAll_clicked())); //显示所有菜肴信息
}

void WelcomeWorker::submit() //根据临时表信息更新数据库表信息
{
    workerAddMenuModel->database().transaction(); //开始事务操作
    if (workerAddMenuModel->submitAll())
    {
        workerAddMenuModel->database().commit(); //提交
        QMessageBox::about(this, tr("修改数据库"),tr("提交成功!"));
    }
    else
    {
        workerAddMenuModel->database().rollback(); //回滚
        QMessageBox::warning(this, tr("adminAddUserModel"),tr("数据库错误: %1").arg(workerAddMenuModel->lastError().text()));
    }
}

void WelcomeWorker::submitWorkerEnter_clicked() //增加新的菜肴
{
    QString mname,material,mprice;
    int rowNum,ok;

    /*获取新菜肴信息*/
    mname = leWorkerMenuName->text();
    mprice = leWorkerMenuPrice->text();
    material = leWorkerMenuMaterial->text();
    rowNum = workerAddMenuModel->rowCount(); //当前行数

    ok = (mname.isEmpty())||((int)(mprice.isEmpty()));
    if(ok)
    {
        QMessageBox::critical(this,tr("错误!"),tr("菜肴名称或者价格为空!"));
    }
    else
    {
        workerAddMenuModel->insertRow(rowNum); //将新的菜肴信息先保存在workerAddMenuModel中
        workerAddMenuModel->setData(workerAddMenuModel->index(rowNum,1),mname);
        workerAddMenuModel->setData(workerAddMenuModel->index(rowNum,2),mprice);
        workerAddMenuModel->setData(workerAddMenuModel->index(rowNum,3),material);
        ok = QMessageBox::warning(this,tr("添加菜肴"),tr("确定添加吗？"),QMessageBox::Yes,QMessageBox::No);
        if(ok == QMessageBox::No)
        {
             workerAddMenuModel->revertAll(); //如果不删除，则撤销
        }
        else
            submit(); //提交信息，将新菜肴信息写入数据库表中
    }
}

void WelcomeWorker::submitWorkerMenuDelete_clicked() //删除选定菜肴信息
{
    int curRow = tvWorkerMenu->currentIndex().row(); //获取选中的行
    workerAddMenuModel->removeRow(curRow);  //删除该行
    int ok = QMessageBox::warning(this,tr("删除当前行!"),tr("你确定删除当前行吗？"),QMessageBox::Yes,QMessageBox::No);
    if(ok == QMessageBox::No)
    {
       workerAddMenuModel->revertAll(); //如果不删除，则撤销
    }
    else
        workerAddMenuModel->submitAll(); //执行删除操作，并在数据库中删除该行
}

void WelcomeWorker::submitWorkerMenuChange_clicked() //根据视图更新数据库表信息
{
    submit();
}

void WelcomeWorker::submitWorkerMenuSearch_clicked()//根据匹配信息进行模糊匹配菜肴信息
{
    QString mname = submitWorkerMenuSName->text();
    if(mname.isEmpty())
    {
        workerAddMenuModel->setFilter(QObject::tr("mname like '%1'").arg("%")); //根据姓名进行筛选
        workerAddMenuModel->select();
    }
    else
    {
        workerAddMenuModel->setFilter(QObject::tr("mname like '%1'").arg((QString)("%")+mname+(QString)("%"))); //根据姓名进行筛选
        workerAddMenuModel->select();
    }
}

void WelcomeWorker::submitWorkerOrderSearchAll_clicked() //根据数据库信息更新视图信息
{
    workerSearchOrderModel->select();
}
